<?php
/**
 * Associations Helper
 *
 * @author Jon Bennett
 * @license MIT
 * @version 0.2 
 */
class AssociationsHelper extends Helper
{
    var $helpers = array('html','form');

	var $__out = array();

	/*
	usage:
	
	* $tags = $this->Tag->generateList() or similar
	* $data['Tag'] = associated HABTM data
	
	echo $associations->habtmCheckBoxes($tags, $data['Tag'], 'Tag', 'p');
	
	*/
	function habtmCheckBoxes($rows=array(), $selectedArr=array(), $modelName, $wrapTag='p')
	{
		// clear output
		$this->__out = array();
		
		// make selected array a workable array
		$selected = array();
		
		if (!empty($selectedArr) && count($selectedArr) > 0)
		{
			foreach ($selectedArr as $arr)
			{
				if (isset($arr['id'])) $selected[] = $arr['id'];
			}
		}
			
		foreach($rows as $id => $title)
		{
			if (in_array($id, $selected))
			{
				$this->__out[] = '<'.$wrapTag.'><input type="checkbox" value="'.$id.'" name="data['.$modelName.']['.$modelName.'][]" checked="checked"/> &nbsp;&nbsp;&nbsp;'.$title.'</'.$wrapTag.'>';
			}
			else
			{
				$this->__out[] = '<'.$wrapTag.'><input type="checkbox" value="'.$id.'" name="data['.$modelName.']['.$modelName.'][]"/> &nbsp;&nbsp;&nbsp;&nbsp;'.$title.'</'.$wrapTag.'>';
			}
		}
		return join("\n", $this->__out);
	}
}
         
